package com.etc.demo.utils;

import javax.annotation.processing.Completion;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class JDBC {
    //链接对象
    private static Connection con = null;

    private static final String URL = "jdbc:mysql://localhost:3306/teXt";
    private static final String username = "root";
    private static final String password = "12345678";

    //静态代码
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    //获取连接对象
    public static Connection getCon() throws SQLException {
        if (con == null){
            con = DriverManager.getConnection(URL, username, password);
        }
        return con;
    }

    //关闭资源
    public void closeConnection(Connection con) throws SQLException {
        con.close();
    }
}
